package com.gzone.contentprovider.helper;

import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

public class DataHelper {
	
private static final String DATABASE_NAME = "contentprovider.v.01.db";
private static SQLiteDatabase db;
	
	public static void initialize(Context context)
	{
		//Open a SQLite Database
		db = context.openOrCreateDatabase(DATABASE_NAME, SQLiteDatabase.CREATE_IF_NECESSARY, null);
	}
	
	public static void createTable(String tableSql)
	{
		try
		{
			//begin the transaction
			db.beginTransaction();
			
			// Create a table
			/* String tableSql= "CREATE TABLE IF NOT EXISTS " + tableName + " ("
					+ "id INTEGER PRIMARY KEY AUTOINCREMENT," + "title TEXT," + "customer TEXT," + "specialist TEXT," +"comments TEXT"
					+ ");";*/
			db.execSQL(tableSql);
			
			//this makes sure transaction is committed
			db.setTransactionSuccessful();
		} 
		finally
		{
			db.endTransaction();
		}
	}
	
	public static void dropTable(String tableName)
	{
		boolean drop = false;
		if(drop){
			try
			{
				//begin the transaction
				db.beginTransaction();
				
				// Create a table
				String tableSql= "DROP TABLE '" + tableName  + "';";
				db.execSQL(tableSql);
				
				//this makes sure transaction is committed
				db.setTransactionSuccessful();
			} 
			finally
			{
				db.endTransaction();
			}
		}
	}
	
	public static boolean isTableEmpty(String table)
	{
		Cursor cursor = null;
		try
		{
			cursor = db.rawQuery("SELECT count(*) FROM "+table, null);
			
			int countIndex = cursor.getColumnIndex("count(*)");
			cursor.moveToFirst();
			int rowCount = cursor.getInt(countIndex);
			if(rowCount > 0)
			{
				return false;
			}
			
			return true;
		}
		finally
		{
			if(cursor != null)
			{
				cursor.close();
			}
		}
	}
	
	public static void insert(String insert, Object[] bindArgs)
	{
		try
		{
			db.beginTransaction();
			//insert this row
/*			String title = ticket.getTitle();
			String customer = ticket.getCustomer();
			String specialist = ticket.getSpecialist();
			String comments = ticket.getComments();
			String insert = "INSERT INTO tickets "+" (title,customer,specialist,comments) VALUES (?,?,?,?);";
*/			db.execSQL(insert,bindArgs);
			db.setTransactionSuccessful();
		}catch(Exception ex){
			ex.printStackTrace();
		}
		finally
		{
			db.endTransaction();
		}
	}
	
	public static void update(String update, Object[] bindArgs)
	{
		try
		{
			db.beginTransaction();
			//insert this row
			/*			String title = ticket.getTitle();
			String customer = ticket.getCustomer();
			String specialist = ticket.getSpecialist();
			String comments = ticket.getComments();
			String insert = "INSERT INTO tickets "+" (title,customer,specialist,comments) VALUES (?,?,?,?);";
			 */
			db.execSQL(update,bindArgs);
			 
			 db.setTransactionSuccessful();
		}
		finally
		{
			db.endTransaction();
		}
	}
	
	public static void delete(String table, int id)
	{
		try
		{
			db.beginTransaction();
			//delete this record
			String delete = "DELETE FROM " + table + " WHERE id='"+ id +"';";
			db.execSQL(delete);
			
			db.setTransactionSuccessful();
		}
		finally
		{
			db.endTransaction();
		}
	}
	public static void deleteAllRow(String table)
	{
		try
		{
			db.beginTransaction();
			//delete this record
			String delete = "DELETE FROM " + table + ";";
			db.execSQL(delete);
			
			db.setTransactionSuccessful();
		}
		finally
		{
			db.endTransaction();
		}
	}
	
	public static Cursor readAll(String table){
		return db.rawQuery("SELECT * FROM " + table, null);
	}
	
	public static Cursor findByParam(String table, String column, Object value){
		String query = "select * from " + table + " where " + column + " = '" + value +"'";
		return db.rawQuery(query , null);
	}
	
	public static Cursor rawQuery(String query, String[] selectionArgs){
		return db.rawQuery(query , selectionArgs);
	}
	
	public static void close(){
		db.close();
	}
}
